package com.waimai.app.qa.support;

import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;

/**
 * 集成Sikuli, 利用Sikuli图片识别技术解决部分元素不识别下的操作
 * @author AiHui
 */
public class SikuliExecutor {

	public static Screen screen;
	public SikuliExecutor(){
		screen = new Screen();
	}
	
	/**
	 * TimeOut时间内,等待目标图片出现且单击
	 * @param regionImagePath
	 * @param timeOut
	 * @return true or false
	 * @author aihui
	 */
	public boolean screenWaitAndClick(String regionImagePath, int timeOut) {
		boolean result = false;
		try {
			screen.exists(regionImagePath, timeOut);
			screen.click(regionImagePath, 0);
			result = true;
		} catch (FindFailed e) {
			result = false;
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 单击指定的图片
	 * @param imgPath
	 * @author aihui
	 */
	public void screenClick(String imgPath){
		try {
			screen.click(imgPath,0);
		} catch (FindFailed e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * TimeOut时间内,等待目标图片消失
	 * @param imgPath
	 * @param timeOut
	 * @return true or false
	 * @author aihui
	 */
	public boolean screenWaitVisible(String imgPath,int timeOut){
		boolean result = false;
		try {
			screen.wait(imgPath, timeOut);
			result = true;
		} catch (FindFailed e) {
			// TODO Auto-generated catch block
			result = false;
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 识别指定的图片,输入指定的字符串
	 * @param regionImagePath
	 * @param str
	 * @author aihui
	 */
	public void screenType(String regionImagePath,String str){
		try {
			if(screen.exists(regionImagePath) != null){
				screen.type(null, str, 0);
			}
		} catch (FindFailed e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*//Add image path  
	Pattern image = new Pattern("C:\\searchButton.png");
	//Wait 10ms for image 
	screen.wait(image, 10);
	//Click on the image
	screen.click(image);*/
}	
	

